﻿namespace Tester
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Windows.Forms;
    using LHA.Parsing.HTML;

    public partial class TesterUI : Form
    {
        private const string SOURCE_FILE_PATH = @"c:\_DEV\Projects\HTMLParser\Tester\Data\test.html";
        private const string SOURCE_FILE_PATH_TEMPLATE = @"c:\_DEV\Projects\HTMLParser\Tester\Data\";

        public TesterUI()
        {
            InitializeComponent();
        }

        private string ProcessSource(string FilePath)
        {
            StreamReader reader = new StreamReader(FilePath);
            string result = reader.ReadToEnd();
            reader.Close();

            return result;
        }

        private string GetTestFile()
        {
            return this.rdbTestFile.Checked ? "Test.html" : "argos.html";
        }

        private void btnStartTest_Click(object sender, EventArgs e)
        {
            this.txtOutput.Clear();
            int counter = 0;

            Parser p = new Parser();
            List<TagItem> tags = p.Parse(this.ProcessSource(SOURCE_FILE_PATH_TEMPLATE + this.GetTestFile()));
            foreach (TagItem ti in tags)
            {
                this.txtOutput.Text += string.Format("{0}: {1}{2}", counter++, ti.Name, "\r\n");
                if (ti.Attributes.Count == 0)
                {
                    this.txtOutput.Text += string.Format("NO ATTRIBUTES.\r\n");
                }
                else
                {
                    this.txtOutput.Text += string.Format("ATTRIBUTES: \r\n");
                    foreach (KeyValuePair<string, string> attribute in ti.Attributes)
                    {
                        this.txtOutput.Text += string.Format("key: {0}, value: {1}{2}", attribute.Key, attribute.Value, "\r\n");
                    }
                }
            }
        }

    }
}
